<template>
	<view>
		<fa-navbar title=""></fa-navbar>
		<view class="py-6 px-2">
			<h1 class="font32 col-main">完善个人信息</h1>
			<view class="py-1 fa-user-form">
				<u-form :model="formData" ref="uForm">
					<u-form-item label="" left-icon=" iconfont icon-shouji" :border-bottom="false">
						<u-input v-model="formData.userName" :custom-style="{'font-size': '32rpx'}" height="80" placeholder="输入个人姓名" maxlength="10"/>
					</u-form-item>
					<u-form-item label="" left-icon=" iconfont icon-mima" :border-bottom="false">
						<u-input v-model="formData.password" :custom-style="{'font-size': '32rpx'}" height="80" placeholder="设置密码" maxlength="12" type="password"/>
					</u-form-item>
					<u-form-item label="" left-icon=" iconfont icon-mima" :border-bottom="false">
						<u-input v-model="formData.repeatPassword" :custom-style="{'font-size': '32rpx'}" height="80" placeholder="再次输入密码" maxlength="12" type="password"/>
					</u-form-item>
				</u-form>
			</view>
			<view class="font24" style="color: #999;" :class="{'col-error': errorTips != '6-12位数字或字母以及组合'}" v-if="errorTips">{{ errorTips }}</view>
			<view class="pt-5">
				<u-button 
					:type="!formValid ? 'default' : 'primary'"
					shape="circle"
					class="btn-grad" 
					:class="formValid ? '' : 'btn-disable'"
					:disabled="!formValid"
					@click="confrim"
				>提交</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import faNavbar from '@/components/fa-navbar/fa-navbar.vue'
	import rules from '@/util/plugins/rules.js'
	import HttpAuth from '@/util/plugins/digest_app.js'
	export default {
		components: {
			faNavbar
		},
		data() {
			return {
				errorTips: '6-12位数字或字母以及组合',
				formData: {
					mobile: '',
					userName: '',
					password: '',
					repeatPassword: ''
				}
			}
		},
		computed: {
			// 表单检验
			formValid() {
				return this.formData.userName.length > 1 && this.formData.password.length > 5 && this.formData.repeatPassword.length > 5
			}
		},
		onLoad(options) {
			this.formData.mobile = options.phone;
		},
		methods: {
			// 注册用户
			confrim() {
				if (!rules['name'].test(this.formData.userName)) {
					return this.errorTips = '请输入2-10位字符，不能包含标点符号以及特殊字符的姓名'
				}
				if (!rules['passw'].test(this.formData.password) || !rules['passw'].test(this.formData.repeatPassword)) {
					return this.errorTips = '请设置6-12位数字或字母以及组合的密码'
				}
				if (this.formData.password != this.formData.repeatPassword) {
					return this.errorTips = '两次密码输入不一致，请重新输入'
				}
				this.errorTips = '6-12位数字或字母以及组合';
				
				uni.showLoading({ title: '提交中' });
				
				let body = {
					input: {
						Mobile: this.formData.mobile,
						Password: this.formData.password,
						Sex: "",
						NameCn: this.formData.userName
					}
				}
					
				HttpAuth.request({
				    type: "POST", //请求类型，GET或POST
				    url: "/api/fasg/Register/CreateFasgUser?servicekey=Register", //请求地址
				    authType: "Digest", //认证类型，支持Diget与Basic，为空会自动判断
				    username: "admin", //用户名
				    password: "cba#834451", //密码
				    data: body,
				    contentType: "application/json; charset=UTF-8", //Content-Type
				    context: this, //回调函数上下文
				    success: (res) => {
						if (res.code === 0) {
							uni.showToast({ title: '注册成功，请登录', icon: 'none' })
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/login/login'
								})
							}, 500);
						}else {
							uni.showToast({ title: res.message, icon: 'none' })
						}
				    },
				    error: (err) => {
				        console.log("401 error");
				    }
				})
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	.col-error {
		color: #E55D5D !important;
	}
</style>
